package com.gzx.plugin.website.articles.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.plugin.website.articles.bo.GzxWebsiteArticlesAddOrEditBo;
import com.gzx.plugin.website.articles.bo.GzxWebsiteArticlesOutQueryBo;
import com.gzx.plugin.website.articles.bo.GzxWebsiteArticlesQueryBo;
import com.gzx.plugin.website.articles.entity.GzxWebsiteArticles;
import com.gzx.plugin.website.articles.mapper.GzxWebsiteArticlesMapper;
import com.gzx.plugin.website.articles.service.GzxWebsiteArticlesService;
import com.gzx.plugin.website.articles.vo.GzxWebsiteArticlesOutVo;
import com.gzx.plugin.website.articles.vo.GzxWebsiteArticlesVo;
import com.gzx.plugin.website.configs.entity.GzxWebsiteConfig;
import com.gzx.plugin.website.configs.mapper.GzxWebsiteConfigMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.mvc.service.impl.BaseServiceImpl;

/**
 * @author zjt
 * @description
 * @date 2025/4/22 14:09
 */
@Service
@DS("website")
public class GzxWebsiteArticlesServiceImpl extends BaseServiceImpl<GzxWebsiteArticlesAddOrEditBo, GzxWebsiteArticlesVo, GzxWebsiteArticlesQueryBo, GzxWebsiteArticlesMapper, GzxWebsiteArticles> implements GzxWebsiteArticlesService {
    public GzxWebsiteArticlesServiceImpl(GzxWebsiteArticlesMapper baseMapper, GzxWebsiteConfigMapper websiteConfigMapper) {
        super(baseMapper);
        this.websiteConfigMapper = websiteConfigMapper;
    }

    private final GzxWebsiteConfigMapper websiteConfigMapper;

    @Override
    protected LambdaQueryWrapper<GzxWebsiteArticles> buildQueryWrapper(GzxWebsiteArticlesQueryBo queryBo) {
        return Wrappers.<GzxWebsiteArticles>lambdaQuery().orderByDesc(GzxWebsiteArticles::getId);
    }

    /**
     * 官网查询文章
     *
     * @param websiteArticlesOutQueryBo
     * @return
     */
    @Override
    public Page<GzxWebsiteArticlesOutVo> getArticlesByType(GzxWebsiteArticlesOutQueryBo websiteArticlesOutQueryBo) {
        GzxWebsiteConfig websiteConfig = websiteConfigMapper.selectOne(Wrappers.<GzxWebsiteConfig>lambdaQuery()
                .select(GzxWebsiteConfig::getId)
                .eq(GzxWebsiteConfig::getHost, websiteArticlesOutQueryBo.getAsa()));
        if (websiteConfig != null) {
            Page<GzxWebsiteArticles> pageResult = baseMapper.selectPage(new Page<>(websiteArticlesOutQueryBo.getCurrent(), websiteArticlesOutQueryBo.getSize()),
                    Wrappers.<GzxWebsiteArticles>lambdaQuery()
                            .select(GzxWebsiteArticles::getId, GzxWebsiteArticles::getTitle, GzxWebsiteArticles::getCreateTime)
                            .eq(GzxWebsiteArticles::getConfigId, websiteConfig.getId())
                            .eq(GzxWebsiteArticles::getType, websiteArticlesOutQueryBo.getDsa())
                            .orderByDesc(GzxWebsiteArticles::getId));
            return (Page<GzxWebsiteArticlesOutVo>) pageResult.convert(entity -> {
                GzxWebsiteArticlesOutVo vo = new GzxWebsiteArticlesOutVo();
                BeanUtils.copyProperties(entity, vo);
                vo.setId(String.valueOf(entity.getId()));
                return vo;
            });
        }
        return new Page<>();
    }

    /**
     * 根据文章ID获取文章内容
     *
     * @param id
     * @return
     */
    @Override
    public String getArticlesContentById(Long id) {
        return baseMapper.selectById(id).getContent();
    }
}
